const express = require("express")();
const expressStatic = require("express-static");
let mysql=require("mysql");
let url =require("url");
const port=56;
// 测试
express.get("/",(req,res)=>{
    res.end("haha")
})
// 与数据库通信
let sql=mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"123456",
    database:"oupu"
})
sql.connect();
express.get("/searchData",(rep,res)=>{
    // 获取数据库
    let id = url.parse(rep.url,true).query.id;
    sql.query(`SELECT * FROM list`,function(error,data){
        if(error){
           res.end(error)
           console.log("传输失败")
        }else{
            res.end(JSON.stringify(data))
            console.log("传输成功")
        }

    })
})
// 详情页获取数据
express.get("/searData",(rep,res)=>{
    // 获取数据库
    let id = url.parse(rep.url,true).query.id;
    sql.query(`SELECT * FROM list WHERE id="${id}"`,function(error,data){
        if(error){
           res.end(error)
           console.log("传输失败")
        }else{
            res.end(JSON.stringify(data))
            console.log("传输成功")
        }

    })
})
// 注册页面写入数据库
express.get("/addData",(rep,res)=>{
    // 获取数据库
    let id =url.parse(rep.url,true).query.id;
    let tel = url.parse(rep.url,true).query.tel;
    let password = url.parse(rep.url,true).query.password;
    sql.query(`INSERT INTO oupu (tel,password) VALUES ("${tel}","${password}")`,function(error,data){
        if(error){         
          console.log(error)
          
           console.log("传输失败")
        }else{
            res.end(JSON.stringify(data))
            console.log("shujuchuanshu is ok")
        }

    })
})
express.get("/userData",(rep,res)=>{
    // 获取数据库
    let id =url.parse(rep.url,true).query.id;
    let tel = url.parse(rep.url,true).query.tel;
    let password = url.parse(rep.url,true).query.password;
    sql.query(` SELECT * FROM oupu WHERE tel="${tel}"`,function(error,data){
        if(error){         
          console.log(error)
          
           console.log("传输失败")
        }else{
            res.end(JSON.stringify(data))
            console.log("shujuchuanshu is ok")
        }

    })
})
// 注册页确认数据库是否有该数据
express.get("/sureData",(rep,res)=>{
    // 获取数据库
    let tel = url.parse(rep.url,true).query.tel;
    sql.query(`SELECT  * FROM oupu WHERE tel='${tel}'`,function(error,data){
        if(error){
           res.end(error)
           console.log("传输失败")
        }else{
            res.end(JSON.stringify(data))
            console.log("shujuchuanshu is ok")
        }

    })
})
express.use(expressStatic(__dirname + '/public'))
express.listen(port)
console.log(`数据传送在${port}端口`)